from operator import le
import requests

from service.blog import *
from service.weibo_info import *


WEIBO_HEADERS = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47 "
}


class Comment:
    def __init__(self, cookies: dict) -> None:
        self.cookies = cookies

    def get_all_comments_by_user_id_blog_id(self, user_id: int, blog_id: int) -> list:
        comment_list = []
        max_id = 1
        while max_id != 0:
            time.sleep(1)
            url = "https://weibo.com/ajax/statuses/buildComments"
            params = {
                "flow": 0,
                "is_reload": 1, 
                "id": blog_id, 
                "is_show_bulletin": 2, 
                "is_mix": 0, 
                "count": 10, 
                "uid": user_id
            }
            if max_id != 1:
                params["max_id"] = max_id

            response = requests.get(url=url, params=params, cookies=self.cookies, headers=WEIBO_HEADERS)
            if response.status_code != 200:
                break

            response = response.json()
            content_data = response["data"]
            if len(content_data) == 0:
                break

            for content in content_data:
                comment_list.append(CommentInfo(
                    comment_id=int(content["id"]),
                    father_comment_id=0,
                    blog_id=blog_id,
                    nick_name=content["user"]["name"],
                    release_time=content["created_at"],
                    content=content["text_raw"]
                ))
                print("\t" + content["user"]["name"] + ": " + content["text_raw"])
                if len(content["comments"]) > 0:
                    children = self.get_children_comments(int(content["id"]), user_id, blog_id)
                    for child in children:
                        comment_list.append(child)

            max_id = response["max_id"]

        return comment_list

    def get_children_comments(self, comment_id: int, user_id: int, blog_id: int) -> list:
        children = []
        max_id = 1
        
        while max_id != 0:
            time.sleep(1)
            url = "https://weibo.com/ajax/statuses/buildComments"
            params = {
                "flow": 0,
                "is_reload": 1, 
                "id": comment_id, 
                "is_show_bulletin": 2, 
                "is_mix": 1, 
                "fetch_level": 1,
                "count": 20, 
                "uid": user_id
            }
            if max_id != 1:
                params["max_id"] = max_id

            response = requests.get(url=url, params=params, cookies=self.cookies, headers=WEIBO_HEADERS)
            if response.status_code != 200:
                break
            
            response = response.json()
            content_data = response["data"]
            if len(content_data) == 0:
                break

            for content in content_data:
                print("\t\t" + content["user"]["name"] + ": " + content["text_raw"])
                children.append(CommentInfo(
                    comment_id=int(content["id"]),
                    father_comment_id=comment_id,
                    blog_id=blog_id,
                    nick_name=content["user"]["name"],
                    release_time=content["created_at"],
                    content=content["text_raw"]
                ))
            max_id = response["max_id"]
        
        return children


if __name__ == "__main__":
    co = Comment(get_cookies())
    co.get_comments_by_user_id_blog_id(1, 1)